// pages/xgxx/xgxx.js
const db = wx.cloud.database()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        openid: '',
        userInfo: '',
        array: ["大一", "大二", "大三", "大四", "专科", "研究生", "已毕业"],
        array1: ["计算机学院", "化学学院", "外国语学院"],
        xy_index: 0,
        nj_index: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // 获取openid
        const app = getApp()
        var openid = app.globalData.openid
        this.setData({
            openid: openid
        })
        // 获取数据库数据
        db.collection("users").where({ _openid: this.data.openid }).get().then(res => {
            console.log(res.data, '28')
            this.setData({
                userInfo: res.data
            })
            for (let i = 0; i < this.data.array.length; i++) {
                if (this, this.data.array[i] == this.data.userInfo[0].nj) {
                    console.log(i)
                    this.setData({
                        nj_index: i
                    })
                }
            }
            for (let i = 0; i < this.data.array1.length; i++) {
                if (this, this.data.array1[i] == this.data.userInfo[0].xy) {
                    console.log(i)
                    this.setData({
                        xy_index: i
                    })
                }
            }
        })
    },
    // 学院选择器
    bindchangenj: function (e) {
        console.log(e.detail.value)
        this.setData({
            nj_index: e.detail.value
        })
    },
    //   选择器，年级
    bindchangexy: function (e) {
        console.log(e.detail.value)
        this.setData({
            xy_index: e.detail.value
        })
    },
    //点击修改按钮，表单提交
    formSubmit(e) {
        db.collection("users").where({ _openid: this.data.openid }).update({
            data: {
                nj: this.data.array[this.data.nj_index],
                phone: e.detail.value.phone,
                username: e.detail.value.name,
                xy: this.data.array1[this.data.xy_index],
                studentid: e.detail.value.xh,
                _updateTime: Date.parse(new Date()),
            },
            success: function (res) {
                wx.showToast({
                    title: '提交成功',
                    icon: 'success',
                    duration:2000,
                    success: function () {
                        wx.navigateTo({
                            url: '../grzl/grzl',
                        })
                    }
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})